package edu.umn.cs5115.scheduler.entities;

import edu.umn.cs5115.scheduler.SchedulerDocument;
import edu.umn.cs5115.scheduler.framework.ManagedData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class MeetingTime extends ManagedData
{
	private Date startDate;		// e.g. 09/05/2006
	private Date endDate;       // e.g. 12/13/2006
	private Date startTime;     // e.g. 11:15
	private Date endTime;       // e.g. 12:30
	private String location;    // e.g. TCEASTBANK
	private EnumSet<Weekday> meetingDays;  // a set of meeting days e.g. { Tuesday, Thursday }
	private Section section;
	
	public static final String START_DATE_KEY   = "startDate";
	public static final String END_DATE_KEY     = "endDate";
	public static final String START_TIME_KEY   = "startTime";
	public static final String END_TIME_KEY     = "endTime";
	public static final String LOCATION_KEY     = "location";
	public static final String MEETING_DAYS_KEY = "meetingDays";
	
	public MeetingTime(SchedulerDocument document, Section section)
	{
		super(document);
		this.section = section;
		meetingDays = EnumSet.noneOf(Weekday.class);
	}
	
	/**
	 * Returns the parent section.
	 * @return The section that this is a meeting time for.
	 */
	public Section getParent()
	{
		return section;
	}
        
	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}
	/**
	 * @return the endTime
	 */
	public Date getEndTime() {
		return endTime;
	}
	/**
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}
	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}
	/**
	 * @return the startTime
	 */
	public Date getStartTime() {
		return startTime;
	}
	/**
	 * @return the meetingDays
	 */
	public EnumSet<Weekday> getMeetingDays() {
            return meetingDays;
	}
	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
		didChangeValueForKey(END_DATE_KEY);
	}
	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
		didChangeValueForKey(END_TIME_KEY);
	}
	/**
	 * @param location the location to set
	 */
	public void setLocation(String location) {
		this.location = location;
		didChangeValueForKey(LOCATION_KEY);
	}
	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
		didChangeValueForKey(START_DATE_KEY);
	}
	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
		didChangeValueForKey(START_TIME_KEY);
	}
	
	public void addMeetingDay(Weekday day)
	{
		this.meetingDays.add(day);
		didChangeValueForKey(MEETING_DAYS_KEY);
	}
	
	/**
	 * Returns the string representation of MeetingTime in the following format:
	 * 09:05 A.M. - 09:55 A.M. , Th (09/05/2006 - 12/13/2006)
	 */
	public String toString() {
		StringBuffer result = new StringBuffer();
		
		SimpleDateFormat formatter = new SimpleDateFormat();
		formatter.applyPattern("hh:mm a");
		
		// append the start/end times         e.g.  09:05 A.M. - 09:55 A.M 
                if (this.getStartTime() != null && this.getEndTime() != null)
                {
                    result.append( formatter.format( this.getStartTime() ) );
                    result.append( " - " );
                    result.append( formatter.format( this.getEndTime() ) );
                    result.append( ", " );
                }
		
		// append the days of the week        e.g.  M,W,F
		Iterator<Weekday> iter = this.getMeetingDays().iterator();
		while (iter.hasNext()) {
			result.append(iter.next());
			if ( iter.hasNext() ) result.append(", ");
		}
		
		// append the start and end date      e.g. (09/05/2006 - 12/13/2006)
		formatter.applyPattern("MM/dd/yyyy");
		result.append("(");
		result.append(formatter.format( this.getStartDate() ) );
		result.append( " - " );
		result.append(formatter.format( this.getEndDate() ) );
		result.append(")");
		
		result.append(" at ");
		result.append(this.getLocation());
		
		return result.toString();
	}
}
